package cn.anosi.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @Description: 日志拦截，分析service性能
 * @author 刘彦硕
 * @date 2017年1月9日 下午5:10:56
 */
@Aspect
@Component
public class LogInterceptor {
	
	private static  Logger logger = LoggerFactory.getLogger(LogInterceptor.class);  
	
	@Around("execution(* cn.anosi.service.*.*.*(..))")
	public Object around(ProceedingJoinPoint point) throws Throwable{
		logger.debug("####　日志拦截开始");
		//拦截的类名
		Object target = point.getTarget();
		long start = System.currentTimeMillis();
		Object object =  point.proceed();
		long end = System.currentTimeMillis();
		long executeTime = end - start;
		Object[] params = point.getArgs();
		StringBuffer buffer = new StringBuffer();
		if(params.length > 0){
			for(int i = 0; i < params.length; i++){
				buffer.append(String.valueOf(params[i]));
				if(i < params.length - 1){
					buffer.append(",");
				}
			}
		}
		//执行的类
		String className = target.getClass().getName();
		String methodName = point.getSignature().getName();
		//返回类型
		String returnType = null;
		if(object != null){
			returnType = object.getClass().getName();
		}
		
		logger.debug("#### 执行的类: " + className);
		logger.debug("#### 方法: " + methodName);
		logger.debug("#### 返回值: " + returnType);
		logger.debug("#### 参数: " + buffer.toString());
		logger.debug("#### 执行时间: " + executeTime + "毫秒");
		logger.debug("#### 日志拦截结束");
		
		return object;
	}
}
